<template>
  <el-dialog :close-on-click-modal="false" modal-append-to-body lock-scroll top="10%" v-if="phone"
             class= "dislog-login dislog-email"
             @close="close"
             :title="$t('public.tip')" :visible.sync="phone.isShow">
    <p>{{$t('login.phoneTip')}}</p>
    <el-input size="mini" :placeholder="$t('sms.enterYourPhone')" v-model.trim="mobile" auto-complete="off"></el-input>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :disabled="btn" @click.native.stop="signInByPhone">{{signName}}</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import { checkform } from '@/utils/validate'
  import { deepClone } from '@/utils/m7Utils.js'
  import NodeRSA from 'node-rsa'
  let md5 = require('md5')
  export default {
    name: 'Phone',
    data () {
      return {
        btn: false,
        mobile: ''
      }
    },
    props: {
      phone: {
        type: Object,
        default: function () {
          return {
            isShow: false,
            _id: '',
            form: null
          }
        }
      }
    },
    computed: {
      signName () {
        return this.btn ? this.$t('public.confirming') : this.$t('public.confirm')
      }
    },
    methods: {
      close () {
        this.btn = false
        this.mobile = ''
      },
      signInByPhone () {
        this.btn = true
        if (!this.mobile.length) {
          this.$message.error(this.$t('validate.phone'))
          this.btn = false
          return null
        }
        let myreg = checkform(this.mobile, 'Number')
        if (myreg) {
          this.btn = false
          this.$message.error(this.$t(myreg))
          return null
        }
        let data = {
          mobile: this.mobile,
          _id: this.phone._id,
          logType: 'bind'
        }
        this.$store.dispatch('bindPhoneBeforeLogin', data).then(val => {
          if (val.success) {
            if (!isMD5(this.phone.form.password)) {
              this.phone.form.password = md5(this.phone.form.password)
            }
            this.$store.dispatch('getLoginSecretKey', {}).then(res1 => {
              if (res1.success) {
                let key = new NodeRSA(res1.secret)
                let _form = deepClone(this.phone.form)
                let encrypted = key.encrypt(_form.password + ':' + res1.num, 'base64')
                _form.safeKey = encrypted
                // delete _form.password
                this.$store.dispatch('signIn', _form).then(val => {
                  let ls = window.localStorage
                  let form = {}
                  if (this.phone.form.remember) {
                    form = this.phone.form
                  } else {
                    form = this.phone.form
                    form.loginName = ''
                    form.password = ''
                  }
                  ls.setItem('loginForm', JSON.stringify(form))
                  if (val.success) {
                    this.$emit('dialogSuccess')
                  }
                  if (val.lastCodeKey && val.message === 'login_send_email_authcode') {
                    this.$emit('emailAuthCode', val)
                    this.phone.isShow = false
                  } else {
                    this.$router.replace({path: '/index'})
                    this.phone.isShow = false
                  }
                })
              }
            })
          } else {
            if (val.message === 'same_account_agent_phone_repeat') {
              this.$message.error('该号码已被其他座席绑定，请检查您的号码！')
            }
            this.btn = false
          }
        })
      }
    }
  }
  function isMD5 (str) {
    let md5 = /^[a-f0-9]{32}$/
    return md5.test(str)
  }
</script>
<style lang="stylus" scoped>
</style>
